package com.hospital.hms.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import com.hospital.hms.common.CommonResult;
import com.hospital.hms.controller.form.*;
import com.hospital.hms.controller.form.SelectMedicalDeptListForm;
import com.hospital.hms.pojo.MedicalDept;
import com.hospital.hms.service.MedicalDeptService;
import com.hospital.hms.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/medical/dept")
@CrossOrigin(origins = "http://localhost:4000", allowCredentials = "true")
@Tag(name = "MedicalDeptController", description = "科室管理接口")
public class MedicalDeptController {
    @Autowired
    MedicalDeptService medicalDeptService;

    @Operation(summary = "获取所有科室名称和ID接口")
    @GetMapping("/selectAll")
    @SaCheckLogin
    public CommonResult selectAll() {
        ArrayList<HashMap> result = medicalDeptService.selectAll();
        return CommonResult.ok().put("result", result);
    }

    @Operation(summary = "获取所有科室和对应的门诊信息接口")
    @GetMapping("/selectDeptAndSub")
    @SaCheckLogin
    public CommonResult selectDeptAndSub() {
        HashMap result = medicalDeptService.selectDeptAndSub();
        return CommonResult.ok().put("result", result);
    }

    @Operation(summary = "分页查询显示科室信息接口")
    @PostMapping("/selectConditionByPage")
    @SaCheckLogin
    public CommonResult selectConditionByPage(@RequestBody @Valid SelectMedicalDeptByPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        param.put("start", start);
        PageUtils pageUtils = medicalDeptService.selectConditionByPage(param);
        return CommonResult.ok().put("result", pageUtils);
    }

    @Operation(summary = "获取所有科室名称和ID")
    @GetMapping("/selectAllDeptNameAndId")
    @SaCheckLogin
    public CommonResult selectAllDeptNameAndId() {
        ArrayList<HashMap> result = medicalDeptService.selectAllDeptNameAndId();
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }


    @Operation(summary = "添加科室接口")
    @PostMapping("/insert")
    @SaCheckLogin
    public CommonResult insert(@RequestBody @Valid InsertMedicalDeptForm form) {
        MedicalDept medicalDept = BeanUtil.toBean(form, MedicalDept.class);  //将form转换成字节码为MedicalDept的Bean实例
        medicalDeptService.insert(medicalDept);
        return CommonResult.ok();
    }

    @Operation(summary = "根据id查询科室信息接口")
    @PostMapping("/selectById")
    @SaCheckLogin
    public CommonResult selectById(@RequestBody @Valid SelectMedicalDeptByIdForm form) {
        HashMap hashMap = medicalDeptService.selectById(form.getId());
        return CommonResult.ok(hashMap);
    }

    @Operation(summary = "修改科室信息接口")
    @PostMapping("/update")
    @SaCheckLogin
    public CommonResult selectById(@RequestBody @Valid UpdateMedicalDeptForm form) {
        MedicalDept medicalDept = BeanUtil.toBean(form, MedicalDept.class);
        medicalDeptService.update(medicalDept);
        return CommonResult.ok();
    }

    @Operation(summary = "删除科室信息接口")
    @PostMapping("/deleteByIds")
    @SaCheckLogin
    public CommonResult deleteByIds(@RequestBody @Valid DeleteMedicalDeptByIdsForm form) {
        medicalDeptService.deleteByIds(form.getIds());
        return CommonResult.ok();
    }


    /*
     * 以下是移动端
     * */
    @PostMapping("/selectMedicalDeptList")
    @Operation(summary = "获取科室列表")
    public CommonResult selectMedicalDeptList(@RequestBody @Valid SelectMedicalDeptListForm form) {
        Map param = BeanUtil.beanToMap(form);
        ArrayList<HashMap> list = medicalDeptService.selectMedicalDeptList(param);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, list);
    }
}
